package com.guigu.wenzhiyi.controller;

import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.guigu.tankai.mapper.MemberMapper;
import com.guigu.tankai.pojo.Member;
import com.guigu.wenzhiyi.pojo.Bodytest;
import com.guigu.wenzhiyi.pojo.Bodytype;
import com.guigu.wenzhiyi.pojo.DuiXiangs;
import com.guigu.wenzhiyi.service.BodytestService;
import com.guigu.wenzhiyi.service.BodytypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DecimalFormat;
import java.util.List;

@RestController
@RequestMapping("Bodytype")
public class BodytypeController {

    @Autowired
    BodytypeService bodytypeService;
    @Autowired
    BodytestService bodytestService;
    @Autowired
    MemberMapper memberMapper;

    // 判断体质的方法
    public static int TiZhi(List<Bodytest> bodytests) {
        int zhi = 0;  // 判断条件使用
        int max = 0; // 总条数
        int zong = 0; // 选中的总分数
        DecimalFormat df = new DecimalFormat("#.00");

        for (Bodytest bodytest : bodytests) {
            // 一条的分数
            zong += Integer.parseInt(df.format((double) bodytest.getOptions() / 5).replace(".", ""));
            max += 1;
        }
        zhi += Integer.parseInt(df.format((double) zong / (max * 100)).replace(".", ""));
        if (zhi >= 60) {
            return 0;
        } else {
            return 1;
        }
    }
    public static List<Bodytest> TiMu(List<Bodytest> bodytests, int type, int Bid) {
        for (Bodytest bodytest : bodytests) {
            bodytest.setType(type);
            bodytest.setBodytypepeid(Bid);
        }
      return bodytests;
    }


    @RequestMapping("/page")
    public PageInfo<Bodytype> page(Integer num, Integer size, String name) {
        return bodytypeService.page(num, size, (name.equals("") ? null : name));
    }

    @RequestMapping("/cha")
    public List<Member> cha(@RequestBody Member member) {
        return bodytypeService.memberList(member);
    }

    @RequestMapping("/add")
    public int add(@RequestBody DuiXiangs duiXiangs) {
        Member m = new Gson().fromJson(duiXiangs.getMember(), Member.class);
        List<Bodytest> yangxus = new Gson().fromJson(duiXiangs.getYangxus(), new TypeToken<List<Bodytest>>() {
        }.getType());
        List<Bodytest> yinxus = new Gson().fromJson(duiXiangs.getYinxus(), new TypeToken<List<Bodytest>>() {
        }.getType());
        List<Bodytest> qixus = new Gson().fromJson(duiXiangs.getQixus(), new TypeToken<List<Bodytest>>() {
        }.getType());

        Bodytype bodytype = new Bodytype();
        bodytype.setMemberid(m.getId());
        // 阳虚体质的结果
        bodytype.setYangquality(TiZhi(yangxus));
        // 阴虚体质的判断
        bodytype.setYindeficiency(TiZhi(yinxus));
        // 阴虚体质的判断
        bodytype.setQiasthenia(TiZhi(qixus));

        // 添加 体质类型表
        int id = bodytypeService.add(bodytype);
        //添加 体质测试 （题目）
        bodytestService.insert(TiMu(yangxus,1,id));
        bodytestService.insert(TiMu(yinxus,2,id));
        bodytestService.insert(TiMu(qixus,3,id));

        m.setCheckitemzt(1);
        memberMapper.zt(m);
        return id;
    }

    @RequestMapping("/update")
    public int update(@RequestBody DuiXiangs duiXiangs) {
        Bodytype m = new Gson().fromJson(duiXiangs.getMember(), Bodytype.class);
        List<Bodytest> yangxus = new Gson().fromJson(duiXiangs.getYangxus(), new TypeToken<List<Bodytest>>() {
        }.getType());
        List<Bodytest> yinxus = new Gson().fromJson(duiXiangs.getYinxus(), new TypeToken<List<Bodytest>>() {
        }.getType());
        List<Bodytest> qixus = new Gson().fromJson(duiXiangs.getQixus(), new TypeToken<List<Bodytest>>() {
        }.getType());

        // 阳虚体质的结果
        m.setYangquality(TiZhi(yangxus));
        // 阴虚体质的判断
        m.setYindeficiency(TiZhi(yinxus));
        // 阴虚体质的判断
        m.setQiasthenia(TiZhi(qixus));

        // 添加 体质类型表
        int id = bodytypeService.update(m);
        //添加 体质测试 （题目）
        bodytestService.updates(yangxus);
        bodytestService.updates(yinxus);
        bodytestService.updates(qixus);

        return id;
    }

    @RequestMapping("/delete")
    public int delete(Integer id){
        int num = bodytestService.delete(id);
        bodytypeService.delete(id);
        Member member = new Member();
        member.setId(id);
        member.setCheckitemzt(0);
        memberMapper.zt(member);
        return num;
    }
}
